﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using CYZHAPI.Common.Config;
using CYZHAPI.Common.DB;
using CYZHAPI.IRepository;
using CYZHAPI.IRepository.IService;
using CYZHAPI.Middlewares;
using CYZHAPI.Repository;
using CYZHAPI.Repository.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace CYZHAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddMemoryCache();
            services.AddOptions();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            // 使用配置文件来注册实例
            services.AddOptions().Configure<List<WebApiOption>>(Configuration.GetSection("WebAPI"));
            services.Configure<WebApiOption>(Configuration.GetSection("WebAPI"));
            //services.Configure<SiteConfig>(Configuration.GetSection("SiteConfig"));
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            //获取配置
            WebApiOption config = serviceProvider.GetService<IOptions<WebApiOption>>().Value;
            
            AddCorsService(services,config);
            AddDBService(services,config);
            services.Configure<FormOptions>(x => {
                x.ValueLengthLimit = 800000000;
                x.MultipartBodyLengthLimit = 1073741822; // In case of multipart
                //x.MultipartBodyLengthLimit = 800000000;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            
            //配置Cors
            app.UseCors(WebApiOption.CORS_POLICY_NAME);
            //api authorized middleware
            //app.UseApiAuthorized();
            app.UseMvc();
            
            
        }
        
        private void AddDBService(IServiceCollection services,WebApiOption config)
        {
           
            //设置全局配置
            services.AddSingleton<IDapperContext>(_ => new DapperContext(
                config
            ));
            services.AddScoped<IUserLoginRep, UserLoginRep>();
            services.AddScoped<IUserLoginService, UserLoginService>();
            services.AddScoped<IRolRep, RolRep>();
            services.AddScoped<IRolService, RolService>();
            services.AddScoped<IAscRep,AscRep>();
            services.AddScoped<IAscService,AscService>();
            services.AddScoped<ISpiResultRep,SpiResultRep>();
            services.AddScoped<ISpiResultService,SpiResultService>();
            services.AddScoped<IModisRep,ModisRep>();
            services.AddScoped<IModisService,ModisService>();
            services.AddScoped<IRasterResultRep,ResterResultRep>();
            services.AddScoped<IRasterResultService,RasterResultService>();
            services.AddScoped<ICantonRep,CantonRep>();
            services.AddScoped<ICantonService,CantonService>();

        }
#region 跨域
        private void AddCorsService(IServiceCollection services, WebApiOption config)
        {
            //配置跨域处理
            /*services.AddCors(options =>{
                options.AddPolicy("any", builder =>{
                    builder.WithOrigins("http://localhost:8080")//AllowAnyOrigin() //允许任何来源的主机访问
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();//指定处理cookie
                });
            });
            var urls = Configuration["AppConfig:Cores"].Split(',');
            services.AddCors(options =>
                options.AddPolicy("AllowSameDomain",
                    builder => builder.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin().AllowCredentials())
            );*/
            if(config.Cors.Original.Equals("*")){
                services.AddCors(options =>
                {
                    options.AddPolicy(WebApiOption.CORS_POLICY_NAME, builder =>
                    {
                        builder.AllowAnyOrigin()
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials();
                    });
                });
            }else{
                var urls = config.Cors.Original.Split(',');
                services.AddCors(options =>
                {
                    options.AddPolicy(WebApiOption.CORS_POLICY_NAME, builder =>
                    {
                        builder.WithOrigins(urls)
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials();
                    });
                });
            }
            //添加cors 服务
            
            
        }
#endregion          
    }
  
}
